import { getAddress, getBanners, getNotices, getProducts, getCategory } from '../../api/home'
import { getToken, setToken, removeToken, setRoles, getRoles, removeRoles } from '../../utils/auth'
// import logo from '@/assets/img/logo.jpg'
const home = {
  state: {
    addresses: [],
    total:0,
    banners: [],
		notices: [],
		products_a: [],
		products_b: [],
		products_c: [],
		category: [],
  },

  mutations: {
		SET_ADDRESSES(state, addresses) {
      state.addresses = addresses
    },
		ADD_ADDRESSES(state, addresses) {
      state.addresses = state.addresses.concat(addresses||[])
    },
		SET_TOTAL(state, total) {
      state.total = total
    },
		SET_BANNER(state, banners) {
      state.banners = banners
    },
		SET_NOTICE(state, notices) {
      state.notices = notices
    },
		SET_PRODUCT_A(state, products) {
      state.products_a = products
    },
		SET_PRODUCT_B(state, products) {
      state.products_b = products
    },
		SET_PRODUCT_C(state, products) {
      state.products_c = products
    },
		SET_CATEGORY(state, category) {
      state.category = category
    },
    SET_AxiosTimeout: (state, axiosTimeout) => {
      state.axiosTimeout = axiosTimeout
    }
  },

  actions: {
		getAddress({ commit }, param) {
      return new Promise((resolve, reject) => {
        getAddress(param).then(data => {
          let [error, res]  = data;
          res.data.pois.forEach((item,index)=>{
            if(item.tel.indexOf(";")>-1){
              item.tel = item.tel.split(";")[0];
            }
            if(item.tel.indexOf(",")>-1){
              item.tel = item.tel.split(",")[0];
            }
            if(Array.isArray(item.tel)){
              item.tel = '';
            }
            // console.log(Array.isArray(item.tel));
          })
          if(param.page===1){
            commit('SET_ADDRESSES', res.data.pois)
          }else{
            commit('ADD_ADDRESSES', res.data.pois)
          }
          if(res.data.count!=="0"){
            commit('SET_TOTAL', res.data.count)
          }
          
          // console.log("total:"+res.data.count)
          resolve(res.data)
        }).catch(error => {
					console.log(error);
          reject(error.data)
        })
      })
    },
		getBanners({ commit }) {
      return new Promise((resolve, reject) => {
        getBanners().then(data => {
          let [error, res]  = data;
          commit('SET_BANNER', res.data.banners)
          resolve(res.data)
        }).catch(error => {
					console.log(error);
          reject(error.data)
        })
      })
    },
		getNotices({ commit }, param) {
      return new Promise((resolve, reject) => {
        getNotices(param).then(data => {
          let [error, res]  = data;
          commit('SET_NOTICE', res.data.notices)
          resolve(res.data)
        }).catch(error => {
					console.log(error);
          reject(error.data)
        })
      })
    },
		getProducts({ commit }, param) {
      return new Promise((resolve, reject) => {
        getProducts(param).then(data => {
          let [error, res]  = data;
          commit('SET_PRODUCT_A', res.data.good_products)
          commit('SET_PRODUCT_B', res.data.hot_products)
          commit('SET_PRODUCT_C', res.data.recently_products)
          resolve(res.data)
        }).catch(error => {
					console.log(error);
          reject(error.data)
        })
      })
    },
		getCategory({ commit }, param) {
      return new Promise((resolve, reject) => {
        getCategory(param).then(data => {
          let [error, res]  = data;
          commit('SET_CATEGORY', res.data.categories)
          resolve(res.data)
        }).catch(error => {
					console.log(error);
          reject(error.data)
        })
      })
    },
  }
}

export default home
